package com.oneclick.tools.applock;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.oneclick.tools.applock.pattern.ChooseLockPattern;
import com.oneclick.tools.applock.pattern.LockPatternUtils;
import com.viewpagerindicator.R;
import com.viewpagerindicator.TitlePageIndicator;
import com.viewpagerindicator.TitlePageIndicator.IndicatorStyle;
import com.viewpagerindicator.TitleProvider;

public class MainActivity extends FragmentActivity {
	private TitleFragmentAdapter mAdapter;
	private ViewPager mPager;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		boolean savedPattern = new LockPatternUtils(getApplicationContext()).savedPatternExists();
		if(!savedPattern) {
			
			Intent intent = new Intent(getApplicationContext(), ChooseLockPattern.class);
			startActivity(intent);
		}
		
		mAdapter = new TitleFragmentAdapter(getSupportFragmentManager());

		mPager = (ViewPager) findViewById(R.id.pager);
		mPager.setAdapter(mAdapter);

		TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicator);
		indicator.setViewPager(mPager);

		final float density = getResources().getDisplayMetrics().density;
		indicator.setBackgroundColor(0xFF000000);
		indicator.setFooterColor(0xFFEE3333);
		indicator.setFooterLineHeight(2 * density);
		indicator.setFooterIndicatorStyle(IndicatorStyle.Triangle);
		indicator.setTextColor(0xFFBBBBBB);
		indicator.setSelectedColor(getResources().getColor(R.color.white));
		indicator.setSelectedBold(false);
		
		Intent intent = new Intent(getApplicationContext(), CoreService.class);
		startService(intent);
	}

	public class TitleFragmentAdapter extends FragmentPagerAdapter implements TitleProvider {
		protected final String[] CONTENT = new String[] { "App Lock",
				"Settings" };

		private int mCount = CONTENT.length;

		public TitleFragmentAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			Fragment fragment = null;
			switch (position) {
			case 0:
				fragment = new AppLockFragment();
				break;
			case 1:
				fragment = new SettingsFragment();
				break;
			default:
				break;
			}
			
			return fragment;
		}

		@Override
		public int getCount() {
			return mCount;
		}

		public void setCount(int count) {
			if (count > 0 && count <= 10) {
				mCount = count;
				notifyDataSetChanged();
			}
		}

		@Override
		public String getTitle(int position) {
			return CONTENT[position % CONTENT.length];
		}
	}
	

	static class TitleFragment extends Fragment {
		private static final String KEY_CONTENT = "TestFragment:Content";
		
		public static TitleFragment newInstance(String content) {
			TitleFragment fragment = new TitleFragment();

			StringBuilder builder = new StringBuilder();
			for (int i = 0; i < 20; i++) {
				builder.append(content).append(" ");
			}
			builder.deleteCharAt(builder.length() - 1);
			fragment.mContent = builder.toString();
			
			return fragment;
		}
		
		private String mContent = "???";
		
		
		
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
			if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
				mContent = savedInstanceState.getString(KEY_CONTENT);
			}
			
			
			
			TextView text = new TextView(getActivity());
			text.setGravity(Gravity.CENTER);
			text.setText(mContent);
			text.setTextSize(20 * getResources().getDisplayMetrics().density);
			text.setPadding(20, 20, 20, 20);
			
			LinearLayout layout = new LinearLayout(getActivity());
			layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			layout.setGravity(Gravity.CENTER);
			layout.addView(text);
			
			return layout;
		}

		@Override
		public void onSaveInstanceState(Bundle outState) {
			super.onSaveInstanceState(outState);
			outState.putString(KEY_CONTENT, mContent);
		}
	}

}